package com.wsoft.utils;

import cn.hutool.cron.pattern.CronPattern;
import cn.hutool.cron.pattern.CronPatternUtil;
import com.wsoft.core.exception.Asserts;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 任务调度
 * @Author z001
 * @Date 2024/10/23 08:59
 */
public class JobUtil {

    private JobUtil() {
    }

    /**
     * 校验cron表达式 是否合法
     *
     * @param cron
     * @return
     */
    public static void checkCron(String cron) {
        try {
            new CronPattern(cron);
        } catch (Exception e) {
            // 如果表达式无效，会抛出ParseException
            Asserts.fail("[job.cron.error]");
        }
    }

    /**
     * 查询未来N次执行时间 从最后一次时间时间 开始计算
     *
     * @param triggerType
     * @param triggerVal
     * @param lastExecuteTime
     * @param num
     * @return
     */
    public static List<LocalDateTime> queryNextTimeFromLast(String triggerVal,
                                                            LocalDateTime lastExecuteTime,
                                                            int num) {
        List<LocalDateTime> nextTimeList = null;
        nextTimeList = JobUtil.queryNextTime(triggerVal, lastExecuteTime, num);
        return nextTimeList;
    }

    /**
     * 查询未来N次执行时间 从当前时间 开始计算
     *
     * @param triggerType
     * @param triggerVal
     * @param lastExecuteTime
     * @param num
     * @return
     */
    public static List<LocalDateTime> queryNextTimeFromNow(String triggerVal,
                                                           int num) {
        LocalDateTime nowTime = LocalDateTime.now();
        List<LocalDateTime> nextTimeList = null;
        nextTimeList = JobUtil.queryNextTime(triggerVal, nowTime, num);
        return nextTimeList;

    }

    /**
     * 根据cron表达式 计算N次执行时间
     *
     * @param cron
     * @param startTime
     * @param num
     * @return
     */
    public static List<LocalDateTime> queryNextTime(String cron, LocalDateTime startTime, int num) {
        if (null == startTime) {
            return Collections.emptyList();
        }
        // 解析cron表达式
        CronPattern cronPattern = new CronPattern(cron);
        // 获取当前时间
        // 将LocalDateTime转换为Instant
        Instant instant = startTime.atZone(ZoneId.systemDefault()).toInstant();
        // 将Instant转换为Date
        Date currentTime = Date.from(instant);
        // 存储执行时间的列表
        List<LocalDateTime> executionTimes = new ArrayList<>();

        // 获取后5次的执行时间
        for (int i = 0; i < num; i++) {
            Date nextTime = CronPatternUtil.nextDateAfter(cronPattern, currentTime);
            if (nextTime != null) {
                // 将Date转换为LocalDateTime
                LocalDateTime localDateTime = nextTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                executionTimes.add(localDateTime);
                currentTime = nextTime;
            } else {
                break;
            }
        }

        return executionTimes;
    }



}
